package com.blueheart.music.controller;

import com.blueheart.music.common.ResultUtil;
import com.blueheart.music.consts.MyConst;
import com.blueheart.music.exception.BaseExceptionEnum;
import com.blueheart.music.model.pojo.SongList;
import com.blueheart.music.service.SongListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class SongListController {
    @Autowired
    private SongListService songListService;

    /**
     * 增加歌单
     * @param request
     * @return
     */
    @PostMapping("/songList/addSongList")
    public ResultUtil addSongList(HttpServletRequest request){
        String title = request.getParameter(MyConst.TITLE).trim();
        String pic = request.getParameter(MyConst.SING_PIC).trim();
        String introduction = request.getParameter(MyConst.SING_INTRODUCTION).trim();
        String style = request.getParameter(MyConst.STYLE).trim();
        if (StringUtils.isEmpty(title)||StringUtils.isEmpty(style)){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        if (songListService.checkName(title)==null){
            SongList songList = new SongList();
            songList.setTitle(title);
            songList.setPic(pic);
            songList.setIntroduction(introduction);
            songList.setStyle(style);
            Boolean flag = songListService.addSongList(songList);
            if (flag){
                return ResultUtil.success();
            }
        }
        return ResultUtil.error(BaseExceptionEnum.SONGLIST_EXIST);
    }

    /**
     * 删除歌单
     * @param id
     * @return
     */
    @PostMapping("/songList/deleteSongList")
    public ResultUtil deleteSongList(@RequestParam("id") Integer id){
        if (id>0){
            Boolean flag = songListService.removeSonglist(id);
            if (flag){
                return ResultUtil.success();
            }
            return ResultUtil.error(BaseExceptionEnum.REMOVE_ERROR);
        }
        return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
    }

    /**
     * 更新歌单
     * @param request
     * @return
     */
    @PostMapping("/songList/updateSongList")
    public ResultUtil updateSongList(HttpServletRequest request){
        String id =request.getParameter(MyConst.SING_ID).trim();
        String title = request.getParameter(MyConst.TITLE).trim();
        String pic = request.getParameter(MyConst.SING_PIC).trim();
        String introduction = request.getParameter(MyConst.SING_INTRODUCTION).trim();
        String style = request.getParameter(MyConst.STYLE).trim();
        if (StringUtils.isEmpty(title)||StringUtils.isEmpty(style)){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        SongList songList = new SongList();
        songList.setId(new Integer(id));
        songList.setTitle(title);
        songList.setPic(pic);
        songList.setIntroduction(introduction);
        songList.setStyle(style);
        Boolean flag = songListService.reviseSongList(songList);
        if (flag){
            return ResultUtil.success();
        }
        return ResultUtil.error(BaseExceptionEnum.UPDATE_ERROR);
    }

    /**
     * 查询全部歌单
     * @return
     */
    @GetMapping("/songList/findAll")
    public ResultUtil findAll(){
        List<SongList> list = songListService.findAll();
        if (list.size()>0){
            return ResultUtil.success(list);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 模糊查询歌单
     * @param title
     * @return
     */
    @PostMapping("/songList/findFromName")
    public ResultUtil findFromName(String title){
        if (StringUtils.isEmpty(title)){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        List<SongList> fromName = songListService.findFromName("%"+title+"%");
        if (fromName.size()>0){
            return ResultUtil.success(fromName);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 精准查询
     * @param title
     * @return
     */
    @PostMapping("/songList/findByName")
    public ResultUtil findByName(String title){
        if (StringUtils.isEmpty(title)){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        SongList byName = songListService.findByName(title);
        if (byName!=null){
            return ResultUtil.success(byName);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * 通过id查询歌单
     * @param id
     * @return
     */
    @PostMapping("/songList/findById")
    public ResultUtil findById(Integer id){
        if (id>0){
            SongList byId = songListService.findById(id);
            if (byId!=null){
                return ResultUtil.success(byId);
            }
            return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
        }
        return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
    }
    @PostMapping("/songList/findByStyle")
    public ResultUtil findByStyle(@RequestParam("style") String style){
        if (StringUtils.isEmpty(style)){
            return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
        }
        List<SongList> byStyle = songListService.findByStyle("%" + style + "%");
        if (byStyle.size()>0){
            return ResultUtil.success(byStyle);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }
}
